package com.utooo.controller;

import com.utooo.constant.ResultCodeConstant;
import com.utooo.pojo.dto.common.CreateGroup;
import com.utooo.pojo.dto.common.RestResult;
import com.utooo.pojo.dto.purchase.contract.PurchaseContractDTO;
import com.utooo.pojo.dto.purchase.order.PurchaseOrderDTO;
import com.utooo.pojo.dto.purchase.task.PurchaseTaskTransferDTO;
import com.utooo.pojo.vo.purchase.DemandMaterialDetailVO;
import com.utooo.pojo.vo.purchase.contract.PurchaseContractVO;
import com.utooo.service.purchase.ContractAcceptanceService;
import com.utooo.service.purchase.OrderDeductionService;
import com.utooo.service.purchase.OrderInventoryService;
import com.utooo.service.purchase.OrderInvoiceAssociationService;
import com.utooo.service.purchase.OrderReceiptService;
import com.utooo.service.purchase.OrderReconciliationService;
import com.utooo.service.purchase.OrderReturnService;
import com.utooo.service.purchase.PaymentApplicationService;
import com.utooo.service.purchase.PrepaymentApplicationService;
import com.utooo.service.purchase.PrepaymentRefundService;
import com.utooo.service.purchase.PurchaseContractService;
import com.utooo.service.purchase.PurchaseOrderService;
import com.utooo.service.purchase.PurchaseTaskManagementService;
import com.utooo.service.purchase.RefundApplicationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 采购管理
 *
 * @author CK
 * @date 2025-04-17 09:19:38
 */
@Tag(name = "采购管理")
@RequestMapping("/purchase")
@RestController
public class PurchaseController {

    @Resource
    private PurchaseTaskManagementService purchaseTaskManagementService;

    @Resource
    private PurchaseContractService purchaseContractService;
    @Resource
    private PurchaseOrderService purchaseOrderService;
    @Resource
    private OrderReceiptService orderReceiptService;
    @Resource
    private OrderInventoryService orderInventoryService;
    @Resource
    private OrderReturnService orderReturnService;
    @Resource
    private OrderDeductionService orderDeductionService;
    @Resource
    private ContractAcceptanceService contractAcceptanceService;
    @Resource
    private OrderReconciliationService orderReconciliationService;
    @Resource
    private PrepaymentApplicationService prepaymentApplicationService;
    @Resource
    private PrepaymentRefundService prepaymentRefundService;
    @Resource
    private PaymentApplicationService paymentApplicationService;
    @Resource
    private RefundApplicationService refundApplicationService;
    @Resource
    private OrderInvoiceAssociationService orderInvoiceAssociationService;

    @RequestMapping(value = "/task/isCombined", method = RequestMethod.GET)
    @Operation(summary = "判断多个采购任务是否可以合并")
    public RestResult<Boolean> isCombined(@RequestParam String taskIds) {
        Boolean result = purchaseTaskManagementService.isCombined(taskIds);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG, result);
    }

    @RequestMapping(value = "/task/materials", method = RequestMethod.GET)
    @Operation(summary = "根据采购任务ids获取物料明细列表")
    public RestResult<List<DemandMaterialDetailVO>> getMaterialListByTaskIds(@RequestParam String taskIds) {
        List<DemandMaterialDetailVO> result = purchaseTaskManagementService.getMaterialListByTaskIds(taskIds);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG, result);
    }

    @RequestMapping(value = "/task/addTask", method = RequestMethod.GET)
    @Operation(summary = "根据采购需求生成采购任务")
    public RestResult<Boolean> addTask(@RequestParam String demandId) {
        Boolean b = purchaseTaskManagementService.addTask(demandId);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG, b);
    }

    @RequestMapping(value = "/order/add", method = RequestMethod.POST)
    @Operation(summary = "新增采购订单")
    public RestResult<String> addOrder(@RequestBody @Validated(CreateGroup.class) PurchaseOrderDTO purchaseOrderDTO) {
        return purchaseTaskManagementService.addOrder(purchaseOrderDTO);

    }

    @RequestMapping(value = "/order/get-order-No", method = RequestMethod.GET)
    @Operation(summary = "获取采购订单编号")
    public RestResult<String> getOrderNo() {
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG, purchaseOrderService.generateOrderNo());
    }


    @RequestMapping(value = "/task/transfer", method = RequestMethod.POST)
    @Operation(summary = "采购任务转办")
    public RestResult<Boolean> transferTask(@RequestBody PurchaseTaskTransferDTO purchaseTaskTransferDTO) {
        Boolean result = purchaseTaskManagementService.transferTask(purchaseTaskTransferDTO);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG, result);
    }

    @RequestMapping(value = "/contract/add", method = RequestMethod.POST)
    @Operation(summary = "新增采购合同")
    public RestResult<Boolean> addContract(@RequestBody @Validated(CreateGroup.class) PurchaseContractDTO purchaseOrderDTO) {
        Boolean result = purchaseContractService.addContract(purchaseOrderDTO);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG, result);
    }

    @RequestMapping(value = "/get-related-contracts", method = RequestMethod.GET)
    @Operation(summary = "获取关联合同列表")
    public RestResult<List<PurchaseContractVO>> getRelatedContracts(@RequestParam String supplierId,
                                                                    @RequestParam String companyId) {
        List<PurchaseContractVO> result = purchaseContractService.getRelatedContracts(supplierId, companyId);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG, result);
    }

    @RequestMapping(value = "/order/receipt/add", method = RequestMethod.POST)
    @Operation(summary = "根据订单发货id新增订单收货")
    public RestResult<Void> addOrderReceiptByPlanId(@RequestParam String shipmentId) {
        orderReceiptService.addOrderReceipt(shipmentId);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }

    @RequestMapping(value = "/order/inventory/add", method = RequestMethod.POST)
    @Operation(summary = "新增订单入库，生成订单业务对账单")
    public RestResult<String> addOrderInventory(@RequestParam String inspectionTaskId) {
        return orderInventoryService.addOrderInventory(inspectionTaskId);
    }

    @RequestMapping(value = "/order/return/add", method = RequestMethod.POST)
    @Operation(summary = "新增订单退货")
    public RestResult<String> addOrderReturn(@RequestParam String inspectionTaskId) {
        return orderReturnService.addOrderReturn(inspectionTaskId);
    }

    @RequestMapping(value = "/order/deduction/add", method = RequestMethod.GET)
    @Operation(summary = "新增订单扣款")
    public RestResult<Void> addOrderDeduction(@RequestParam String orderReturnId) {
        orderDeductionService.addOrderDeduction(orderReturnId);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }

    @GetMapping("/order/reconciliation/generate")
    @Operation(summary = "退货流程通过后，生成订单对账记录")
    public RestResult<Void> generateOrderReconciliation(@RequestParam String orderReturnId) {
        orderReconciliationService.generateOrderReconciliation(orderReturnId);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }

    @PostMapping("/order/reconciliation/confirm")
    @Operation(summary = "采购方确认后同步订单对账记录至供货商")
    public RestResult<String> confirmAndAddReconciliation(@RequestParam String id) {
        return orderReconciliationService.confirmAndAddReconciliation(id);
    }

    @PostMapping("/order/reconciliation/withdraw")
    @Operation(summary = "采购方撤回并删除供应商订单对账记录")
    public RestResult<String> withdrawReconciliation(@RequestParam String id) {
        return orderReconciliationService.withdrawReconciliation(id);
    }

    @GetMapping("/contract/acceptance/add")
    @Operation(summary = "采购合同流程通过后生成合同验收记录")
    public RestResult<String> addContractAcceptance(@RequestParam String contractId) {
        return contractAcceptanceService.addContractAcceptance(contractId);
    }

    @PostMapping("/contract/acceptance/confirm")
    @Operation(summary = "采购方确认后同步合同验收记录至供货商")
    public RestResult<String> confirmAndSyncContractAcceptance(@RequestParam String id) {
        return contractAcceptanceService.confirmAndSyncContractAcceptance(id);
    }


    @GetMapping("/get-pre-payment-No")
    @Operation(summary = "获取预付款申请单号")
    public RestResult<String> getPrePaymentNo() {
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG,
                prepaymentApplicationService.getPrePaymentNo());
    }

    @GetMapping("/get-pre-payment-refund-No")
    @Operation(summary = "获取预退款申请单号")
    public RestResult<String> getPrePaymentRefundNo() {
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG,
                prepaymentRefundService.getPrePaymentRefundNo());
    }

    @GetMapping("/get-payment-No")
    @Operation(summary = "获取付款申请单号")
    public RestResult<String> getPaymentNo() {
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG,
                paymentApplicationService.getPaymentNo());
    }

    @GetMapping("/get-refund-No")
    @Operation(summary = "获取退款申请单号")
    public RestResult<String> getRefundNo() {
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG,
                refundApplicationService.getRefundNo());
    }

    @GetMapping("/get-order-invoice-association-No")
    @Operation(summary = "获取订单关联发票开票单号")
    public RestResult<String> getOrderInvoiceAssociationNo() {
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG,
                orderInvoiceAssociationService.getOrderInvoiceAssociationNo());
    }

    @PostMapping("/order/invoice/association/add")
    @Operation(summary = "新增供应商订单关联发票记录")
    public RestResult<Void> addOrderInvoiceAssociation(@RequestParam String id) {
        orderInvoiceAssociationService.addOrderInvoiceAssociation(id);
        return new RestResult<>(ResultCodeConstant.CODE_200, ResultCodeConstant.CODE_200_MSG);
    }
}
